import { Message } from 'element-ui'

type MessageType = 'success' | 'warning' | 'info' | 'error'

const errorStatusMsg: any = {
  400: '当前操作没有权限',
  401: '无效的Token',
  403: '当前操作没有权限',
  404: '未找到该资源',
  405: '请求方法未允许',
  408: '请求超时',
  426: '账号或密码错误',
  478: '验证码错误,请重新输入',
  479: '演示环境，没有权限操作',
  500: '服务器端出错',
  501: '网络未实现',
  502: '网络错误',
  503: '服务不可用',
  504: '网络超时',
  505: 'http版本不支持该请求'
}

function msg(param: any) {
  return Message(param)
}

function base(message: string, type: MessageType) {
  (Message as any).closeAll()
  return Message({
    message,
    type,
    center: true,
    showClose: true
  })
}

msg.success = (v: string) => {
  return base(v, 'success')
}
msg.error = (v: string) => {
  return base(v, 'error')
}
msg.reqerror = (reuqestData: any, msg?: string) => {
  let v = ''
  let errUrl = ''
  // 优先使用状态码类的错误
  if (reuqestData.response) {
    let { data, status } = reuqestData.response
    const url = reuqestData.response.config.url || ''
    status += ''
    errUrl = url.slice(url.indexOf('/', 1))
    data = data || {}
    v = data.data || data.msg || errorStatusMsg[status]
  } else {
    // 请求200的错误
    const data = reuqestData || {}
    v = data.data || data.msg
  }
  v = v || msg || '请求失败'
  // if (errUrl) {
  //   v += `ㅤ${errUrl}`
  // }
  return base(v, 'error')
}
msg.warning = (v: string) => {
  return base(v, 'warning')
}
msg.info = (v: string) => {
  return base(v, 'info')
}

export default msg
